<template>
  <div class='container'>
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button
          style="float:right"
          type="primary"
          size="small"
          @click="search_button()">
          查询搜索
        </el-button>
        <el-button
          style="float:right;margin-right: 15px"
          size="small"
          @click="reset_button()">
          重置
        </el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form :inline="true"  size="small" label-width="140px">
          <el-form-item label="输入搜索收货人：">
            <el-input  class="input-width" v-model="keyword_value" placeholder="收货人姓名" clearable></el-input>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>订单列表</span>
      <el-button :loading="downloadLoading" size="mini" class="btn-add" style="margin-left: 20px; float:right" type="primary" icon="el-icon-document" @click="handleDownload" >
        导出报表
      </el-button>
      <el-button size="mini" class="btn-add" @click="multiple_button()" style="margin-left: 20px; float:right" icon='el-icon-coffee' type="warning">回收站</el-button>
      <el-button size="mini" class="btn-add" @click="multiple_button()" style="margin-left: 20px; float:right" icon='el-icon-s-open' type="warning">验证码核销</el-button>
      <el-button size="mini" class="btn-add" @click="multiple_print_button()" style="margin-left: 20px; float:right" icon='el-icon-document-copy' type="success">批量发货和打印数据单</el-button>
      <el-button size="mini" class="btn-add" @click="multiple_delete_button()" style="margin-left: 20px; float:right" icon='el-icon-delete' type="danger">批量删除</el-button>
      <!-- <el-button size="mini" class="btn-add" @click="add_button()" style="margin-left: 20px; float:right" icon='el-icon-plus' type="primary">添加</el-button> -->
    </el-card>
    <div class='table-container'>
      <el-table
        class='m-30-t'
        :data="order_list"
        fit
        v-loading="listLoading"
        @select="chosen_option"
        @select-all="all_options"
        stripe
        border
        highlight-current-row>
        <el-table-column type="selection" ></el-table-column>
        <el-table-column
          prop="id"
          label="订单id"
          align="left">
        </el-table-column>
        <el-table-column
          prop="accept_name"
          label="收货人"
          align="center">
        </el-table-column>
        <el-table-column
          prop="status"
          label="支付状态"
          align="center">
          <template slot-scope="scope">
            <div v-if="scope.row.status == 1">生成订单</div>
            <div v-if="scope.row.status == 2">已支付订单</div>
            <div v-if="scope.row.status == 3">取消订单(客户触发)</div>
            <div v-if="scope.row.status == 4">作废订单(管理员触发)</div>
            <div v-if="scope.row.status == 5">完成订单</div>
            <div v-if="scope.row.status == 6">退款(订单完成后)</div>
            <div v-if="scope.row.status == 7">部分退款(订单完成后)</div>
            <div v-if="scope.row.status == 8">售后中</div>
            <div v-if="scope.row.status == 9">换货完成</div>
            <div v-if="scope.row.status == 10">已过期</div>
          </template>
        </el-table-column>
        <el-table-column
          prop="status"
          label="发货状态"
          align="center">
          <template slot-scope="scope">
            <div v-if="scope.row.distribution_status == 0">未发货</div>
            <div v-if="scope.row.distribution_status == 1">已发货</div>
          </template>
        </el-table-column>
        <el-table-column prop="type"  label="订单类型" align="center">
          <template slot-scope="scope">
            <div v-if="scope.row.type == 'default'">普通订单</div>
            <div v-if="scope.row.type == 'costpoint'">积分订单</div>
            <div v-if="scope.row.type == 'directpay'">directpay订单</div>
          </template>
        </el-table-column>
        <el-table-column
          prop="create_time"
          label="创建时间"
          align="center">
        </el-table-column>
        <el-table-column  prop="pay_type" label="支付方式"  align="center">
          <template slot-scope="scope">
            <div v-if="scope.row.pay_type == '1'">微信支付</div>
            <div v-if="scope.row.pay_type == '2'">支付宝支付</div>
            <div v-if="scope.row.pay_type == ''">其它支付</div>
          </template>
        </el-table-column>
        <el-table-column
          prop="order_amount"
          label="订单金额"
          align="center">
        </el-table-column>
        <el-table-column
          label="用户id"
          align="center"
          >
          <template slot-scope="scope">
            <span @click="go_user_detail(scope.row.user_id)">{{scope.row.user_id}}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          width="300px">
          <template slot-scope="scope">
            <el-button size="small" type="primary" @click="print_button(scope.row.id)">打印</el-button>
            <el-button size="small" type="warning" @click="detail_button(scope.row.id)">详情</el-button>
            <el-button size="small" type="danger" @click="delete_button(scope.row.id)">删除</el-button>
            <el-button size="small" type="success" @click="edit_button(scope.$index, scope.row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container m-50-t m-50-b">
      <span></span>
      <el-pagination
        background
        @size-change="size_change"
        @current-change="current_change"
        layout="total, sizes,prev, pager, next,jumper"
        :current-page.sync="page_num"
        :page-size="page_size"
        :page-sizes="page_sizes"
        :total="total">
      </el-pagination>
    </div>

    <el-dialog
      :title="isEdit?'编辑':'添加'"
      :visible.sync="dialog_visible"
      width="40%">
      <el-form
               ref="adminForm"
               label-width="150px" size="small">
        <el-form-item   label="订单号：">
          <el-input style="width: 250px" v-model="true_name"></el-input>
        </el-form-item>
        <el-form-item label="创建时间：">
          <el-input style="width: 250px" v-model="status"></el-input>
        </el-form-item>
        <el-form-item label="收货人：">
          <el-input style="width: 250px" v-model="source_id"></el-input>
        </el-form-item>
        <!-- <el-form-item label="生日：">
          <el-input  type="password" style="width: 250px" v-model="birthday"></el-input>
        </el-form-item> -->
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialog_visible = false" size="small">取 消</el-button>
        <el-button type="primary" size="small" @click="confirm_button">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="订单详情"
      width="50%"
      :visible.sync="detail_dialog_visible">
      <div>
        <span class="true_name">订单号：</span>
        <span class="m-10-l ">{{detail_data.order_no}}</span>
      </div>
      <div class=' m-30-t'>
        <span class="true_name">创建时间：</span>
        <span class="m-10-l  ">{{detail_data.create_time}}</span>
      </div>
      <div class=' m-30-t'>
        <span class="true_name">收货人：</span>
        <span class="m-10-l  ">{{detail_data.source_id}}</span>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="detail_dialog_visible = false" size="small">取 消</el-button>
        <el-button @click="detail_dialog_visible = false" type="primary" size="small">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import order_request from '@/api/order_request'
export default {
  name: 'Order',
  data() {
    return {
      detail_data: {},
      order_list: [],
      dialog_visible: false,
      isEdit: false,
      search_value: '',
      page_num: 1,
      page_size: 10,
      page_sizes: [10, 20, 50],
      total: null,
      true_name: '',
      status: '',
      birthday: '',
      source_id: '',
      detail_dialog_visible: false,
      keyword: '',
      keyword_value: '',
      current_size: 10,
      listLoading: false,
      downloadLoading: false,
      current_page: 1,
      tableData: [],
      print_obj: {
        // id: 'print_demo',
        url: './print.vue',
        popTitle: 'good print'
      },
      order_list_all: [],
    }
  },
  created() {
    this.get_list()
    this.get_list_all()
  },
  mounted() {

  },
  methods: {
    cancel_button() {
      const params = {

      }
      order_request.cancel_order(params, res => {

      })
    },
    go_user_detail(id) {
      this.$router.push({ path: '/user/user_detail', query:{id}})

    },
    // 搜索
    search_button() {
      this.current_page = 1
      this.keyword = this.keyword_value
      this.get_list()
    },
    // 重置
    reset_button() {
      this.keyword = ''
      this.keyword_value = ''
      this.get_list()
    },
    // 添加
    add_button() {
      this.isEdit = false
      this.dialog_visible = true
      this.true_name = ''
      this.source_id = ''
      this.status = ''
    },
    // 确认
    confirm_button() {
      const params = {
        true_name: this.true_name,
        source_id: this.source_id,
        status: this.status,
      }
      if (this.isEdit) {
        order_request.edit_order(params, res => {
          console.log(res, this.true_name)
          this.dialog_visible = false
          this.get_list()
        })
      }
      else {
        order_request.add_order(params, res => {
          this.dialog_visible = false
          this.get_list()
        })
      }
    },
    // 删除
    delete_button(id) {
      this.$confirm('是否要进行删除操作?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const params = {
          id
        }
        order_request.delete_order(params, res=> {
          this.get_list()
        })
      }).catch(err => err)
    },
    multiple_print_button() {
      this.$router.push({path: '/order/print'})
    },
    print_button() {
      this.$router.push({path: '/order/print'})
    },
    chosen_option(id_list) {
      this.delete_list = id_list
    },
    all_options(id_list) {
      this.delete_list = id_list
    },
    multiple_delete_button() {
      this.$confirm('是否要进行批量删除操作?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        for (const item of this.delete_list) {
          order_request.delete_order({ id: item.id }, res => {
            this.get_list()
          })
        }
        this.delete_list = []
      }).catch(err=>err)
    },
    // 编辑
    edit_button(index, item) {
      this.$router.push({path: '/order/order_edit', query:{id: item.id, gid: item.goods_no}})
    },
    // 详情
    detail_button(id) {
      this.$router.push({path: '/order/order_detail', query:{id}})
      // order_request.detail_order({id}, res=> {
      //   this.detail_data = res.data
      // })
      // this.detail_dialog_visible = true
    },
    size_change(current_size) {
      this.current_size = current_size
      this.get_list()
    },
    handleDownload() {
      this.downloadLoading = true
      import('@/vendor/Export2Excel').then(excel => {
        const tHeader = ['收货人', '地址', '手机','座机','订单金额','支付状态','下单时间','商品信息']
        const filterVal = ['accept_name','address','mobile','telphone','order_amount','status','create_time','goods_name']
        const list = this.order_list_all
        // console.log(list[0].status);
        // const data = list
        const data = this.formatJson(filterVal, list)
        excel.export_json_to_excel({
          header: tHeader,
          data,
          filename: '清单',
        })
        this.downloadLoading = false
      }).catch(e=>{
        this.downloadLoading = false
      })
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => {
        return filterVal.map(j => {
          if (j === 'status') {
            // v[j] = v[j] == 1?'未支付':v[j] == 2?"已支付":'已完成'
            return v[j]
          } else {
            return v[j]
          }
        })
      })
    },
    get_list_all() {
      this.downloadLoading = true
      order_request.order_list({pageSize: this.page_size, page: this.page_num}, res=> {
        if (res.code == 0) {
          this.downloadLoading = false
          let order_list_all = res.data
          order_list_all = this.only_status_2(order_list_all)
          this.order_list_all = order_list_all
        }
      })
    },
    only_status_2(list) {
      let target_list = []
      for (let i =0;i<list.length;i++) {
        if(list[i].status == 2) {
          list[i].status = '已完成'
          target_list.push(list[i])
        }
      }
      return target_list
    },
    current_change(current_page) {
      this.current_page = current_page
      this.get_list()
    },
    filter_list() {
      order_request.order_list({  pageSize: this.page_size, key: this.keyword }, res => {
        this.order_list = res.data
        this.total = res.data.length
      })
    },
    get_list() {
      this.listLoading = true
      order_request.order_list({ pageSize: this.current_size, page: this.current_page, accept_name: this.keyword }, res => {
        this.listLoading = false
        this.order_list = res.data
        this.total = res.totalRecord
      })
    }
  }
}
</script>

<style scoped>
@import "~@/styles/layout.scss";
.table-container {
  margin-left: 30px;
}
.pagination-container {
  float: right;
}
</style>
